package com.atguigu.gmall.mq.service;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RabbitService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public boolean sendMessage(String exchange, String routingKey, Object message) {
        try {
            rabbitTemplate.convertAndSend(exchange, routingKey, message);
            return true;
        } catch (AmqpException e) {
            e.printStackTrace();
            return false;
        }

    }

    public boolean sendMessage(String exchange, String routingKey, Object message, MessagePostProcessor messagePostProcessor) {
        try {
            rabbitTemplate.convertAndSend(exchange, routingKey, message,messagePostProcessor);
            return true;
        } catch (AmqpException e) {
            e.printStackTrace();
            return false;
        }

    }


}
